package com.iwhalecloud.bss.kite.cucc.web.service.accept.handler.attr;

import com.iwhalecloud.bss.kite.client.dto.DataResult;
import com.iwhalecloud.bss.kite.client.vo.scene.CommonAttrVO;
import com.iwhalecloud.bss.kite.common.constant.KeyConsts;
import com.iwhalecloud.bss.kite.common.constant.ServiceOfferConsts;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.common.constant.LocalKeyConsts;
import com.iwhalecloud.bss.kite.manager.api.IOperateSceneInstService;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttr;
import com.iwhalecloud.bss.kite.manager.inst.KiteSceneInst;
import com.iwhalecloud.bss.kite.web.service.accept.attr.handler.AbstractAttrHandler;
import com.ztesoft.bss.common.util.ContextUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.Objects;

@Service
public class DealHandlerNameAttrHandler extends AbstractAttrHandler {

    @Resource
    private IQuerySceneInstService querySceneInstService;

    @Resource
    private IOperateSceneInstService operateSceneInstService;

    @Override
    public void init(CommonAttrVO commonAttr) {

    }

    @Override
    public DataResult<Boolean> apply(String sceneInstId, String fieldName, String value, String valueDesc) {
        KiteSceneInst sceneInst = querySceneInstService.getSceneInst(sceneInstId);
        String serviceOfferId = sceneInst.getServiceOfferId();
        //重提初始化不校验
        //fixme 可能导致频繁访问缓存,上线临时处理方案
        Map<String, Object> extParams = ContextUtil.getLoginInfo().getUserInfo().getExtParams();
        if (Objects.nonNull(extParams) && extParams.containsKey("nodeCode")) {
            return super.apply(sceneInstId, fieldName, value, valueDesc);
        }

        // 变更业务的场景初始化过程中不校验
        if (!ServiceOfferConsts.SERVICE_OFFER_ORDER.equals(serviceOfferId)
            && !KeyConsts.IFTRUE_T.equals(
            querySceneInstService.getExtParameterValue(sceneInstId, LocalKeyConsts.BUSI_CHANGE_SCENE))) {
            return super.apply(sceneInstId, fieldName, value, valueDesc);
        }
        KiteAttr handlerInfoTel = querySceneInstService.getCommonAttr(sceneInstId, "handler_info_tel");
        if (Objects.nonNull(handlerInfoTel)) {
            String postProvinceNbr = ContextUtil.getLoginInfo().getUserInfo().getPostProvinceNbr();
            // 经办人联系电话是置灰且是上海业务 经办人名称被修改时，清空经办人名称外其他经办人模块属性
            if (KiteStringUtils.equals(LocalKeyConsts.PROVINCE_SHANGHAI, postProvinceNbr) && KiteStringUtils.equals(KeyConsts.IFTRUE_F, handlerInfoTel.getIsEdit())) {
                List<KiteAttr> handlerAttrs = querySceneInstService.listCommonAttrsByModuleId(sceneInstId,
                    LocalKeyConsts.MODULE_ID_OPERATOR);
                handlerAttrs.stream().forEach(handlerAttr -> {
                    if (!KiteStringUtils.equals("handler_info_name", handlerAttr.getFieldName())) {
                        handlerAttr.setIsEdit(KeyConsts.IFTRUE_T);
                        handlerAttr.setValue("");
                        handlerAttr.setValueDesc("");
                    }
                });
                handlerAttrs.forEach(handlerAttr -> operateSceneInstService.setCommonAttr(sceneInstId, handlerAttr));
            }
        }
        return super.apply(sceneInstId, fieldName, value, valueDesc);
    }
}
